home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / mesa-amiwin / src / macros.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  5.9 KB  |  221 lines

  1. /* macros.h */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995  Brian Paul  (brianp@ssec.wisc.edu)
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25. $Id: macros.h,v 1.10 1995/09/13 14:48:43 brianp Exp $
  26.  
  27. $Log: macros.h,v $
  28.  * Revision 1.10  1995/09/13  14:48:43  brianp
  29.  * added NORMALIZE_3V macro
  30.  * added array declaration macros for MacIntosh
  31.  *
  32.  * Revision 1.9  1995/07/24  20:34:46  brianp
  33.  * better MEMSET and MEMCPY macros per Asif Khan
  34.  *
  35.  * Revision 1.8  1995/07/20  15:35:32  brianp
  36.  * added casts to UBYTE_TO_FLOAT, USHORT_TO_FLOAT, UINT_TO_FLOAT
  37.  *
  38.  * Revision 1.7  1995/05/30  15:07:30  brianp
  39.  * added ROUND()
  40.  *
  41.  * Revision 1.6  1995/05/22  20:59:34  brianp
  42.  * Release 1.2
  43.  *
  44.  * Revision 1.5  1995/05/17  13:17:22  brianp
  45.  * changed default CC.Mode value to allow use of real OpenGL headers
  46.  * removed need for CC.MajorMode variable
  47.  *
  48.  * Revision 1.4  1995/05/12  19:18:55  brianp
  49.  * added INSIDE_BEGIN_END macro
  50.  *
  51.  * Revision 1.3  1995/03/17  19:15:14  brianp
  52.  * added ABS macro
  53.  *
  54.  * Revision 1.2  1995/03/04  19:25:29  brianp
  55.  * 1.1 beta revision
  56.  *
  57.  * Revision 1.1  1995/02/24  14:24:52  brianp
  58.  * Initial revision
  59.  *
  60.  */
  61.  
  62.  
  63. /*
  64.  * A collection of useful macros.
  65.  */
  66.  
  67.  
  68. #ifndef MACROS_H
  69. #define MACROS_H
  70.  
  71.  
  72.  
  73. /* Limits: */
  74. #define MAX_GLUSHORT    0xffff
  75. #define MAX_GLUINT    0xffffffff
  76.  
  77.  
  78.  
  79. /* Copy short vectors: */
  80.  
  81. #define COPY_3V( DST, SRC )    DST[0] = SRC[0];    \
  82.                 DST[1] = SRC[1];    \
  83.                 DST[2] = SRC[2];
  84.  
  85. #define COPY_4V( DST, SRC )    DST[0] = SRC[0];    \
  86.                 DST[1] = SRC[1];    \
  87.                 DST[2] = SRC[2];    \
  88.                 DST[3] = SRC[3];
  89.  
  90.  
  91. /* Assign scalers to short vectors: */
  92. #define ASSIGN_3V( V, V0, V1, V2 )  V[0] = V0;  V[1] = V1;  V[2] = V2;
  93.  
  94. #define ASSIGN_4V( V, V0, V1, V2, V3 )  V[0] = V0;    \
  95.                         V[1] = V1;    \
  96.                         V[2] = V2;    \
  97.                         V[3] = V3;
  98.  
  99.  
  100. /* Test if we're inside a glBegin / glEnd pair: */
  101. #define INSIDE_BEGIN_END  (CC.Mode!=GL_BITMAP)
  102.  
  103.  
  104. /* Absolute value: */
  105. #define ABS(X)  ((X) < 0 ? -(X) : (X))
  106.  
  107.  
  108. /* Round a floating-point value to the nearest integer: */
  109. #define ROUND(X)   ( (X)<0.0 ? ((GLint) ((X)-0.5)) : ((GLint) ((X)+0.5)) )
  110.  
  111.  
  112. /* Clamp X to [MIN,MAX]: */
  113. #define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
  114.  
  115.  
  116. /* Min of two values: */
  117. #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
  118.  
  119.  
  120. /* MAX of two values: */
  121. #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
  122.  
  123.  
  124. /* Dot product of two 3-element vectors */
  125. #define DOT3( a, b )  ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] )
  126.  
  127.  
  128. /* Dot product of two 4-element vectors */
  129. #define DOT4( a, b )  ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3] )
  130.  
  131.  
  132. /* Normalize a 3-element vector to unit length */
  133. #define NORMALIZE_3V( a )  { GLfloat len;                \
  134.                              len = sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);    \
  135.                  if (len>0.0001) {                \
  136.                     GLfloat scale = 1.0F / len;        \
  137.                     a[0] *= scale;                \
  138.                                 a[1] *= scale;                \
  139.                                 a[2] *= scale;                \
  140.                  }                        \
  141.                            }
  142.  
  143.  
  144. /*
  145.  * Integer / float conversion for colors, normals, etc.
  146.  */
  147.  
  148. /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
  149. #define UBYTE_TO_FLOAT(B)    ((GLfloat) (B) * (1.0F / 255.0F))
  150.  
  151. /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
  152. #define FLOAT_TO_UBYTE(X)    ((GLubyte) (GLint) (((X)) * 255.0F))
  153.  
  154.  
  155. /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
  156. #define BYTE_TO_FLOAT(B)    ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
  157.  
  158. /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
  159. #define FLOAT_TO_BYTE(X)    ( (((GLint) (255.0F * (X))) - 1) / 2 )
  160.  
  161.  
  162. /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
  163. #define USHORT_TO_FLOAT(S)    ((GLfloat) (S) * (1.0F / 65535.0F))
  164.  
  165. /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
  166. #define FLOAT_TO_USHORT(X)    ((GLushort) (GLint) ((X) * 65535.0F))
  167.  
  168.  
  169. /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
  170. #define SHORT_TO_FLOAT(S)    ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
  171.  
  172. /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
  173. #define FLOAT_TO_SHORT(X)    ( (((GLint) (65535.0F * (X))) - 1) / 2 )
  174.  
  175.  
  176. /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
  177. #define UINT_TO_FLOAT(U)    ((GLfloat) (U) * (1.0F / 4294967295.0F))
  178.  
  179. /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
  180. #define FLOAT_TO_UINT(X)    ((GLuint) (GLint) ((X) * 4294967295.0))
  181.  
  182.  
  183. /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
  184. #define INT_TO_FLOAT(I)        ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
  185.  
  186. /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
  187. #define FLOAT_TO_INT(X)        ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
  188.  
  189.  
  190. /* Memory copy: */
  191. #ifdef SUNOS4
  192. #define MEMCPY( DST, SRC, BYTES) \
  193.     memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) )
  194. #else
  195. #define MEMCPY( DST, SRC, BYTES) \
  196.     memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) )
  197. #endif
  198.  
  199.  
  200. /* Memory set: */
  201. #ifdef SUNOS4
  202. #define MEMSET( DST, VAL, N ) \
  203.     memset( (char *) (DST), (int) (VAL), (int) (N) )
  204. #else
  205. #define MEMSET( DST, VAL, N ) \
  206.     memset( (void *) (DST), (int) (VAL), (size_t) (N) )
  207. #endif
  208.  
  209.  
  210. /* MACs don't support static larger than 32kb, so... */
  211. #ifdef OS_MACINTOSH
  212. #define DEFARRAY(TYPE,NAME,SIZE)  TYPE *NAME = MAC_ALLOC(sizeof(TYPE)*SIZE)
  213. #define UNDEFARRAY(NAME)          MAC_FREE(NAME)
  214. #else
  215. #define DEFARRAY(TYPE,NAME,SIZE)  TYPE NAME[SIZE]
  216. #define UNDEFARRAY(NAME)
  217. #endif
  218.  
  219.  
  220. #endif /*MACROS_H*/
  221.